package com.hdl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hdl.commons.DBUtil;
import com.hdl.dao.SysRoleDao;
import com.hdl.entity.SysRole;

public class SysRoleDaoImpl implements SysRoleDao {

	@Override
	public SysRole select(Integer sysRoleid) {
		String sql="select sys_role_name from sys_role where sys_role_id=?";
		try {
			return DBUtil.get(sql, SysRole.class, sysRoleid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询用户角色失败",e);
		}
	}

	@Override
	public List<SysRole> selectByMode(SysRole role,int index,int len) {
		StringBuffer sql=new StringBuffer("SElECT sys_role_id,sys_role_name from sys_role where 1=1");
		List<Object> pstm=new ArrayList<Object>();
		sql.append(" LIMIT ?,?");
		pstm.add(index);
		pstm.add(len);
		try {
			return DBUtil.list(sql.toString(),SysRole.class, pstm.toArray());
		} catch (SQLException e) {
		
			e.printStackTrace();
			throw new RuntimeException("查找列表失败",e);
		}
		
	}
	

	private SysRole load(ResultSet rs) throws SQLException {
		SysRole sys= new SysRole();
		sys.setSysRoleid(rs.getInt("sys_role_id"));
		sys.setSysRoleName(rs.getString("sys_role_name"));
		
			return sys;
		}
	
	public List<SysRole> selectt(int index, int len) throws SQLException {
		String sql="SELECT * from sys_role  limit ?,?";
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=DBUtil.getConnection();
			pstm=conn.prepareStatement(sql);
			pstm.setObject(1, index);
			pstm.setObject(2, len);
			rs=pstm.executeQuery();
			List<SysRole> list=new ArrayList<>();
			while(rs.next()) {
			
		
				list.add(load(rs));
			}return list;
		
		}finally {
			rs.close();
			pstm.close();
			conn.close();
		}
	}
	

}
